package com.edee.bigday.order.web;

import com.alipay.api.AlipayApiException;
import com.edee.bigday.order.config.AlipayTemplate;
import com.edee.bigday.order.service.OrderService;
import com.edee.bigday.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PayWebController {

    @Autowired
    AlipayTemplate alipayTemplate;
    @Autowired
    OrderService orderService;

    /**
     * 1、让浏览器显示支付页；
     * 2、支付成功后，跳转到用户订单列表页。
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @ResponseBody
    @GetMapping(value = "/payOrder", produces = "text/html") //返回的数据是text/html，而不是application/json
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
//        PayVo payVo = new PayVo();
//        //设置订单信息：
//        payVo.setBody();//商品描述
//        payVo.setOut_trade_no();//订单号
//        payVo.setSubject();//商品名称
//        payVo.setTotal_amount();//交易金额

        //返回的是一个页面，将此页面直接交给浏览器即可：
        PayVo payVo = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println("pay="+pay);
        //return "Successfully payed！";
        return pay;
    }
}
